<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1">


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="description" content="Nest配置Swagger接口文档以及Nest的基本操作"/>




  <meta name="keywords" content="node," />





  <link rel="alternate" href="/default" title="刘粉丝来了">






<link rel="canonical" href="http://yoursite.com/2020/06/15/nestSwagger/"/>


<meta name="description" content="第一步：安装依赖 npm install –save @nestjs&#x2F;swagger swagger-ui-express  第二步：打开main.js做如下配置123456789101112131415161718&#x2F;&#x2F; 引入依赖import &amp;#123; SwaggerModule, DocumentBuilder &amp;#125; from &#39;@nestjs&#x2F;swagger&#39;;async func">
<meta property="og:type" content="article">
<meta property="og:title" content="Nest配置Swagger接口文档以及Nest的基本操作">
<meta property="og:url" content="http://yoursite.com/2020/06/15/nestSwagger/index.html">
<meta property="og:site_name" content="刘粉丝来了">
<meta property="og:description" content="第一步：安装依赖 npm install –save @nestjs&#x2F;swagger swagger-ui-express  第二步：打开main.js做如下配置123456789101112131415161718&#x2F;&#x2F; 引入依赖import &amp;#123; SwaggerModule, DocumentBuilder &amp;#125; from &#39;@nestjs&#x2F;swagger&#39;;async func">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-14T16:09:56.000Z">
<meta property="article:modified_time" content="2020-06-14T16:39:19.094Z">
<meta property="article:author" content="刘粉丝来了">
<meta property="article:tag" content="node">
<meta name="twitter:card" content="summary">


<link rel="stylesheet" type="text/css" href="/css/style.css?v=1.1" />
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet'>





<script type="text/javascript">
  var themeConfig = {
    fancybox: {
      enable: false
    },
  };
</script>




  



    <title> Nest配置Swagger接口文档以及Nest的基本操作 - 刘粉丝来了 </title>
  <meta name="generator" content="Hexo 4.2.0"></head>

  <body>
    <div id="page">
      <header id="masthead"><div class="site-header-inner">
    <h1 class="site-title">
        <a href="/." class="logo">刘粉丝来了</a>
    </h1>

    <nav id="nav-top">
        
            <ul id="menu-top" class="nav-top-items">
                
                    <li class="menu-item">
                        <a href="/">
                            
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/archives">
                            
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/tags">
                            
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/about">
                            
                            
                                About
                            
                        </a>
                    </li>
                
            </ul>
        
  </nav>
</div>

      </header>
      <div id="content">
        
    <div id="primary">
        
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          Nest配置Swagger接口文档以及Nest的基本操作
        
      </h1>

      <time class="post-time">
          6月 15 2020
      </time>
    </header>



    
            <div class="post-content">
            <h3 id="第一步：安装依赖"><a href="#第一步：安装依赖" class="headerlink" title="第一步：安装依赖"></a><strong>第一步：安装依赖</strong></h3><blockquote>
<p>npm install –save @nestjs/swagger swagger-ui-express</p>
</blockquote>
<h3 id="第二步：打开main-js做如下配置"><a href="#第二步：打开main-js做如下配置" class="headerlink" title="第二步：打开main.js做如下配置"></a><strong>第二步：打开<code>main.js</code>做如下配置</strong></h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入依赖</span></span><br><span class="line"><span class="keyword">import</span> &#123; SwaggerModule, DocumentBuilder &#125; <span class="keyword">from</span> <span class="string">'@nestjs/swagger'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">bootstrap</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> app = <span class="keyword">await</span> NestFactory.create(AppModule);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> options = <span class="keyword">new</span> DocumentBuilder()</span><br><span class="line">    .setTitle(<span class="string">'商城API'</span>)        <span class="comment">// 项目API标题</span></span><br><span class="line">    .setDescription(<span class="string">'开源商城项目'</span>)             <span class="comment">// 项目API描述</span></span><br><span class="line">    .setVersion(<span class="string">'1.0'</span>)              <span class="comment">// 版本号</span></span><br><span class="line">    .build();</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> <span class="built_in">document</span> = SwaggerModule.createDocument(app, options);</span><br><span class="line">  <span class="comment">// 这里我设置了 api-docs 访问接口文档时就是访问 http://localhost:5000/api-docs</span></span><br><span class="line">  SwaggerModule.setup(<span class="string">'api-docs'</span>, app, <span class="built_in">document</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">await</span> app.listen(<span class="number">3000</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="第四步：Nest里注释Swagger的一些API"><a href="#第四步：Nest里注释Swagger的一些API" class="headerlink" title="第四步：Nest里注释Swagger的一些API"></a><strong>第四步：Nest里注释Swagger的一些API</strong></h3><blockquote>
<p>@ApiTags(‘会员模块’)      swagger标题分类</p>
</blockquote>
<blockquote>
<p>@ApiOperation({ summary:’删除帖子’ })     表示此路由的功能</p>
</blockquote>
<blockquote>
<p>@ApiProperty({ descripiton:’商品标题’ })      参数描述</p>
</blockquote>
<h2 id="Nest的一些基本操作"><a href="#Nest的一些基本操作" class="headerlink" title="Nest的一些基本操作"></a><strong>Nest的一些基本操作</strong></h2><ul>
<li><code>nest g mo posts</code> 创建posts模型</li>
<li><code>nest g co posts</code> 创建posts控制器</li>
<li><code>npm run start:dev</code>       运行nest项目</li>
<li>@Body() body:string       获取post请求的响应体</li>
<li>@Param(‘id’) id:string    获取用户指定传递的id</li>
<li>@Query() query        获取带在Url后的参数</li>
</ul>
<h2 id="Nest创建模型"><a href="#Nest创建模型" class="headerlink" title="Nest创建模型"></a><strong>Nest创建模型</strong></h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; prop &#125; <span class="keyword">from</span> <span class="string">'@typegoose/typegoose'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Post</span> </span>&#123;</span><br><span class="line">    @prop()</span><br><span class="line">    title: string</span><br><span class="line">    @prop()</span><br><span class="line">    content:string</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Nest效验参数是否为空"><a href="#Nest效验参数是否为空" class="headerlink" title="Nest效验参数是否为空"></a><strong>Nest效验参数是否为空</strong></h2><blockquote>
<p>安装依赖 —  npm add class-validator class-transformer</p>
</blockquote>
<blockquote>
<p>导入依赖 —-  import { IsNotEmpty } from ‘class-validator’</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CreatePostDto</span> </span>&#123; </span><br><span class="line">    @ApiProperty(&#123;<span class="attr">description</span>:<span class="string">'帖子标题'</span>&#125;)</span><br><span class="line">    @IsNotEmpty(&#123;<span class="attr">message</span>:<span class="string">'帖子标题不能为空'</span>&#125;)</span><br><span class="line">    title:string</span><br><span class="line">    @ApiProperty(&#123;<span class="attr">description</span>:<span class="string">'帖子内容'</span>&#125;)</span><br><span class="line">    content:string</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

            </div>
          

    
      <footer class="post-footer">
		
		<div class="post-tags">
		  
			<a href="/tags/node/">node</a>
		  
		</div>
		

        
        
  <nav class="post-nav">
    
    
      <a class="next" href="/2020/06/14/nestMongodb/">
        <span class="next-text nav-default">Nest连接数据库</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

        
  <div class="comments" id="comments">
    
  </div>


      </footer>
    
  </article>


    </div>

      </div>

      <footer id="colophon"><script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
<span class="copyright-year" style="width:100%; display:flex; justify-content: space-between;">
    <span>
        
        &copy;
    
        2014 -
    
    2020
    <span class="footer-author">刘粉丝来了.</span>
    <span class="power-by">
        由 <a class="hexo-link" href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a> and <a class="theme-link" href="https://github.com/frostfan/hexo-theme-polarbear" target="_blank" rel="noopener">Polar Bear</a> 强力驱动
    </span>
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv" style="color:red">24280</span>次
    </span>
</span>



      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>
    


    




  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  

    <script type="text/javascript" src="/js/src/theme.js?v=1.1"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=1.1"></script>


  </body>
</html>
